package com.itheima.ydd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.ydd.common.R;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.dto.SysMenuDto;
import com.itheima.ydd.entity.SysMenu;
import com.itheima.ydd.mapper.SysMenuMapper;
import com.itheima.ydd.service.ISysMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 菜单表 服务实现类
 * </p>
 *
 * @author author
 * @since 2022-05-22
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements ISysMenuService {

    @Override
    public ResultData allData() {
        //查询菜单下的一级目录
        LambdaQueryWrapper<SysMenu> sysMenuLambdaQueryWrapper=new LambdaQueryWrapper<>();
        sysMenuLambdaQueryWrapper.eq(SysMenu::getPid, 0);
        List<SysMenu> list = this.list(sysMenuLambdaQueryWrapper);
        List<SysMenuDto> menuDtoList=new ArrayList<>();

        for (SysMenu sysMenu : list) {
            SysMenuDto sysMenuDto=new SysMenuDto();
            BeanUtils.copyProperties(sysMenu,sysMenuDto);
            //查询二级目录
            LambdaQueryWrapper<SysMenu> sysMenuLambdaQueryWrapper2=new LambdaQueryWrapper<>();
            sysMenuLambdaQueryWrapper2.eq(SysMenu::getPid,sysMenu.getId());
            List<SysMenu> list2 = this.list(sysMenuLambdaQueryWrapper2);
            List<SysMenuDto> menuDtoList2=new ArrayList<>();

            for (SysMenu menu2 : list2) {
                SysMenuDto sysMenuDto2=new SysMenuDto();
                BeanUtils.copyProperties(menu2,sysMenuDto2);
                //查询三级目录
                LambdaQueryWrapper<SysMenu> sysMenuLambdaQueryWrapper3=new LambdaQueryWrapper<>();
                sysMenuLambdaQueryWrapper3.eq(SysMenu::getPid,menu2.getId());
                List<SysMenu> list3 = this.list(sysMenuLambdaQueryWrapper3);
                sysMenuDto2.setChildren(list3);
                menuDtoList2.add(sysMenuDto2);
            }
            sysMenuDto.setChildren(menuDtoList2);
            menuDtoList.add(sysMenuDto);
        }

        return ResultData.ok("",menuDtoList);
    }
}
